import { patchRouter } from '../utils'
import { turnApp } from './routerHandle'

// 重写window的路由跳转
export const rewriteRouter = () => {
  window.history.pushState = patchRouter(window.history.pushState, 'micro_push')
  window.history.replaceState = patchRouter(window.history.replaceState, 'micro_replace')

  window.addEventListener('micro_push', turnApp)
  window.addEventListener('micro_replace', turnApp)
  // pushState 与 replaceState 并不会触发此行为 只有浏览器的前进后退及go方法才会触发
  // https://developer.mozilla.org/zh-CN/docs/Web/API/WindowEventHandlers/onpopstate
  window.addEventListener('popstate', turnApp)
}
